package com.mineoj.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mineoj.common.ErrorCode;
import com.mineoj.constant.CommonConstant;
import com.mineoj.exception.BusinessException;
import com.mineoj.exception.ThrowUtils;
import com.mineoj.model.dto.competitionranking.CompetitionRankingQueryRequest;
import com.mineoj.model.entity.*;
import com.mineoj.service.CompetitionRankingService;
import com.mineoj.mapper.CompetitionRankingMapper;
import com.mineoj.service.UserService;
import com.mineoj.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 明月
* @description 针对表【competition_ranking(比赛排名)】的数据库操作Service实现
* @createDate 2025-04-20 20:23:57
*/
@Service
public class CompetitionRankingServiceImpl extends ServiceImpl<CompetitionRankingMapper, CompetitionRanking>
    implements CompetitionRankingService{

    @Resource
    private UserService userService;



    /**
     * 获取查询包装类
     *
     * @param competitionRankingQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<CompetitionRanking> getQueryWrapper(CompetitionRankingQueryRequest competitionRankingQueryRequest) {
        QueryWrapper<CompetitionRanking> queryWrapper = new QueryWrapper<>();
        if (competitionRankingQueryRequest == null) {
            return queryWrapper;
        }
        String searchText = competitionRankingQueryRequest.getSearchText();
        String sortField = competitionRankingQueryRequest.getSortField();
        String sortOrder = competitionRankingQueryRequest.getSortOrder();
        Long id = competitionRankingQueryRequest.getId();
        Integer ranking = competitionRankingQueryRequest.getRanking();
        Integer score = competitionRankingQueryRequest.getScore();
        Long competitionId = competitionRankingQueryRequest.getCompetitionId();
        Long userId = competitionRankingQueryRequest.getUserId();
        // 拼接查询条件
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.and(qw -> qw.like("ranking", searchText).or().like("score", searchText));
        }
        queryWrapper.eq(ObjectUtils.isNotEmpty(ranking), "ranking", ranking);
        queryWrapper.eq(ObjectUtils.isNotEmpty(score), "score", score);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(competitionId), "competitionId", competitionId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }




}




